/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "net_config.h"
#include "pzcar_wheel_ctrl.h"

extern char* eCm_String[];
extern char* eSt_String[];
extern PzCarInfo g_carInfo;

//Recv msg format:
void ProcessRecvMsg(unsigned char *buff, int msgLen)
{
    CarMode newMode = g_carInfo.e_CM;
    CarState newState = g_carInfo.e_ST;
    int data = 0;

    #define MSG_OFFSET  (5)
    //SSID:OHOS
    //PSWD:12345678
    if (msgLen > MSG_OFFSET) {
        if ((memcmp(buff, "SSID:", MSG_OFFSET) == 0) ||
            (memcmp(buff, "ssid:", MSG_OFFSET) == 0)) {
            memset(ssid, 0, sizeof(ssid));
            memcpy(ssid, &buff[MSG_OFFSET], msgLen-MSG_OFFSET);
            flag |= flag_1;
        } else if ((memcmp(buff, "PSWD:", MSG_OFFSET) == 0) ||
                   (memcmp(buff, "pswd:", MSG_OFFSET) == 0)) {
            memset(pswd, 0, sizeof(pswd));
            memcpy(pswd, &buff[MSG_OFFSET], msgLen-MSG_OFFSET);
            flag |= flag_2;
        }
        if (flag == flag_3) {
            g_carInfo.e_NetST = Net_ST_Config;
        }
    }

    // Mode:Auto
    // Mode:Track
    // Mode:AudCmd
    // Mode:NetCtrl
    if (strncmp(buff, "Mode:", 5) == 0) {
        if (strncmp(buff+5, "Auto", 4) == 0) {
            newMode = eMode_Auto;
        } else if (strncmp(buff+5, "Track", 5) == 0) {
            newMode = eMode_Track;
        } else if (strncmp(buff+5, "AudCmd", 6) == 0) {
            newMode = eMode_AudCmd;
        } else if (strncmp(buff+5, "NetCtrl", 7) == 0) {
            newMode = eMode_NetCtrl;
        }
        if (g_carInfo.e_CM != newMode) {
            g_carInfo.e_CM = newMode;
            g_carInfo.e_ST = eSt_Stop;
        }
    }

    if (g_carInfo.e_CM != eMode_NetCtrl) {
        return;
    }

    // State:Stop
    // State:Forward
    // State:TurnLeft
    // State:TurnRight
    // State:Backward
    // State:BackLeft
    // State:BackRight
    if (strncmp(buff, "State:", 6) == 0) {
        if (strncmp(buff+6, "Stop", 4) == 0) {
            newState = eSt_Stop;
        } else if (strncmp(buff+6, "Forward", 7) == 0) {
            newState = eSt_Forward;
        } else if (strncmp(buff+6, "TurnLeft", 8) == 0) {
            newState = eSt_TurnLeft;
        } else if (strncmp(buff+6, "TurnRight", 9) == 0) {
            newState = eSt_TurnRight;
        } else if (strncmp(buff+6, "Backward", 8) == 0) {
            newState = eSt_Backward;
        } else if (strncmp(buff+6, "BackLeft", 8) == 0) {
            newState = eSt_BackLeft;
        } else if (strncmp(buff+6, "BackRight", 9) == 0) {
            newState = eSt_BackRight;
        }

        if (g_carInfo.e_ST != newState) {
            g_carInfo.e_ST = newState;
        }
    } else if (strncmp(buff, "Speed:", 6) == 0) {
        data = atoi(buff+6);
        if ((data < g_carInfo.d_speed_min) || (data > g_carInfo.d_speed_max)) {
            printf("%s: newSpeed[%d] invalid, speed_curr[%d]\n",
                __func__, data, g_carInfo.d_speed_curr);
            // g_carInfo.d_speed_curr = g_carInfo.d_speed_def;
        } else {
            g_carInfo.d_speed_curr = (short) data;
            printf("%s: newSpeed[%d] valid, update to speed_curr[%d]\n",
                __func__, data, g_carInfo.d_speed_curr);
        }
    } else if (strncmp(buff, "Duration:", 9) == 0) {
        data = atoi(buff+6);
    }
}